package cn.tramy.pinxian.common.handler.registry

import cn.tramy.pinxian.wind.handler.registry.DefaultHandlerRegistry
import cn.tramy.pinxian.wind.handler.registry.HandlerRegistry
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import kotlin.test.assertEquals

internal class DefaultHandlerRegistryTest {
    private lateinit var handlerRegistry: HandlerRegistry

    @BeforeEach
    fun setUp() {
        handlerRegistry = DefaultHandlerRegistry()
    }

    @Test
    fun registerHandler() {
        assertEquals(0, this.handlerRegistry.handlerCount(TestEvent::class.java))
        val handler = TestEventHandler()
        this.handlerRegistry.registerHandler(handler, TestHandler::class.java)
        assertEquals(1, this.handlerRegistry.handlerCount(TestEvent::class.java))
        assertEquals(2, this.handlerRegistry.handlerCount(SubTestEvent::class.java))

    }

    @Test
    fun findHandlers() {
        val handler = TestEventHandler()
        this.handlerRegistry.registerHandler(handler, TestHandler::class.java)
        val handlers = this.handlerRegistry.findHandlers(TestEvent::class.java)
        assertEquals(1, handlers.size)
//        assertEquals(TestEventHandler::class.java.name, handlers.first().instance.javaClass.name)
//        assertEquals("process", handlers.first().handlerMethod.name)
    }
}


open class TestEvent

class SubTestEvent : TestEvent()

class TestEventHandler {

    @TestHandler(async = true)
    fun process(event: TestEvent) {

    }

    @TestHandler
    fun processSub(event: SubTestEvent) {

    }
}

@Retention(AnnotationRetention.RUNTIME)
@Target(AnnotationTarget.FUNCTION)
annotation class TestHandler(val async: Boolean = false)